require('../../../util/common')
var Poker = require('./Poker');

var Cost = {};

Cost[3] = 1;
Cost[4] = 2;
Cost[5] = 3;
Cost[6] = 4;
Cost[7] = 5;
Cost[8] = 6;
Cost[9] = 7;
Cost[10] = 8;
Cost[11] = 9;
Cost[12] = 10;
Cost[13] = 11;
Cost[1] = 12;
Cost[2] = 16;

Cost[22] = 22;
Cost[23] = 33;

function getCost(card){
    if(card > 100){
        return Cost[Poker.getPoint(card)];
    }else {
        return Cost[Poker.getPoint(card) + 20];
    }
}
function getCostCards(cards){
    var costCards = {};
    ExObject.eachKeyNum(cards, function(c, n){
        if (n > 0) {
            var cost = getCost(c);
            if(!costCards[cost]){
                costCards[cost] = {
                    'num': 0,
                    'point': Poker.getPoint(c)
                };
            }
            costCards[cost].num += 1;
        }
    });
    return costCards;
}
function getMaxCost(cards){
    var maxCost = 0;
    ExObject.eachKeyNum(cards, function(c, v){
        if(getCost(c) > maxCost){
            maxCost = getCost(c);
        }
    });
    return maxCost;
}
function getCardsNum(cards){
    return ExObject.sumValue(cards);
}
//王炸
function checkKingBomb(cards){
    var num = getCardsNum(cards);
    var resp = false;
    if(num == 2) {
        if (cards[Poker.SpecPoint.JOKER_B] == 1 && cards[Poker.SpecPoint.JOKER_S] == 1) {
            resp = true;
        }
    }
    return resp;
}
//4张炸
function checkBomb(cards){
    var num = getCardsNum(cards);
    var resp = false;
    if(num == 4){
        var costCards = getCostCards(cards);
        ExObject.eachKeyNum(costCards, function(c, v){
            if(v.num >= 4){
                resp = true;
                return false;
            }
        });
    }
    return resp;
}
//单牌
function checkSingle(cards){
    if(getCardsNum(cards) != 1){
        return false;
    }
    return true;
}
//连对
function checkPairs(cards){
    var num = getCardsNum(cards);

    if(num % 2 != 0){
        return false;
    }
    if(num < 6){
        return false;
    }
    var costCards = getCostCards(cards);
    var maxCost = getMaxCost(cards);

    var resp = true;
    for(var i = maxCost; i > maxCost - num / 2; i--){
        if(!(costCards[i] && costCards[i].num == 2)){
            resp = false;
        }
    }
    return resp;

}
//对子
function checkPair(cards){
    if(getCardsNum(cards) != 2){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = true;
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num != 2){
            resp = false;
            return false;
        }
    });
    return resp;
}
//顺子
function checkRun(cards){
    var num = getCardsNum(cards);
    if(num < 5){
        return false;
    }
    var costCards = getCostCards(cards);
    var maxCost = getMaxCost(cards);
    var resp = true;
    for(var i = maxCost; i > maxCost - num; i--){
        if(!(costCards[i] && costCards[i].num == 1)){
            resp = false;
        }
    }
    return resp;
}
//三条
function checkTriple(cards){
    if(getCardsNum(cards) != 3){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = true;
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num != 3){
            resp = false;
            return false;
        }
    });
    return resp;
}
//三带一
function checkTripleOne(cards){
    if(getCardsNum(cards) != 4){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = false;
    var context = {
        'triple': 0,
        'single': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 3){
            context.triple += 1;
        }
        if(v.num == 1){
            context.single += 1;
        }
    });
    if(context.triple == 1 && context.single == 1) {
        resp = true;
    }
    return resp;
}
//三带二
function checkTripleTwo(cards){
    if(getCardsNum(cards) != 5){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = false;
    var context = {
        'triple': 0,
        'pair': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 3){
            context.triple += 1;
        }
        if(v.num == 2){
            context.pair += 1;
        }
    });
    if(context.triple == 1 && context.pair == 1) {
        resp = true;
    }
    return resp;
}
//飞机
function checkAirplane(cards){
    var num = getCardsNum(cards);
    if(num % 4 != 0){
        return false;
    }
    if(num < 8){
        return false;
    }
    var costCards = getCostCards(cards);

    var resp = false;
    var context = {
        'triple': 0,
        'triples': {},
        'single': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 3){
            context.triple += 1;
            context.triples[c] = 1;
        }else if(v.num == 4){
            context.triple += 1;
            context.triples[c] = 1;
            context.single += 1;
        }else if(v.num){
            context.single += v.num;
        }
    });
    if(tripleIsRun(context.triples, num / 4)){
        resp = true;
    }
    return resp;
}
//飞机带对
function checkAirplaneTwo(cards){
    var num = getCardsNum(cards);
    if(num % 5 != 0){
        return false;
    }
    if(num < 10){
        return false;
    }
    var costCards = getCostCards(cards);

    var resp = false;
    var context = {
        'triple': 0,
        'triples': {},
        'pair': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 3){
            context.triple += 1;
            context.triples[c] = 1;
        }else if(v.num == 2){
            context.pair += 1;
        }else if(v.num == 4){
            context.pair += 2;
        }
    });
    if(tripleIsRun(context.triples, num / 5) && context.triple == context.pair){
        resp = true;
    }
    return resp;
}
//三顺
function checkRunTriple(cards){
    var num = getCardsNum(cards);
    if(num % 3 != 0){
        return false;
    }
    if(num < 6){
        return false;
    }
    var costCards = getCostCards(cards);

    var resp = false;
    var context = {
        'triple': 0,
        'triples': {},
        'single': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 3){
            context.triple += 1;
            context.triples[c] = 1;
        }else if(v.num){
            context.single += v.num;
        }
    });
    if(tripleIsRun(context.triples, num / 3)){
        resp = true;
    }
    return resp;
}
//是否是三顺
function tripleIsRun(triples, num){
    var resp = true;
    var maxCost = 0;
    ExObject.eachKeyNum(triples, function(c, v){
        if(v > 0){
            if(c > maxCost){
                maxCost = c;
            }
        }
    });
    for(var i = maxCost; i > maxCost - num; i--){
        if(triples[i] != 1){
            resp = false;
            break;
        }
    }
    if(resp){
        return resp;
    }else{
        resp = true;
    }
    var minCost = maxCost;
    ExObject.eachKeyNum(triples, function(c, v){
        if(v > 0) {
            if (c < minCost) {
                minCost = c;
            }
        }
    });
    for(var j = minCost; j < minCost + num; j++){
        if(triples[j] != 1){
            resp = false;
            break;
        }
    }
    return resp;
}
//四带二
function checkFourTwo(cards){
    if(getCardsNum(cards) != 6){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = false;
    var context = {
        'four': 0,
        'two': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 4){
            context.four += 1;
        }else {
            context.two += v.num;

        }
    });
    if(context.four == 1 && context.two == 2) {
        resp = true;
    }
    return resp;
}
//四带二对
function checkFourTwoPair(cards){
    if(getCardsNum(cards) != 8){
        return false;
    }
    var costCards = getCostCards(cards);
    var resp = false;
    var context = {
        'four': 0,
        'two': 0
    };
    ExObject.eachKeyNum(costCards, function(c, v){
        if(v.num == 4){
            context.four += 1;
        }else {
            context.two += v.num;

        }
    });
    if(context.four == 1 && context.two == 4) {
        resp = true;
    }else if(context.four == 2){
        resp = true;
    }
    return resp;
}
module.exports = {
    checkBomb: checkBomb,
    getCardsNum: getCardsNum,
    getCostCards: getCostCards,
    checkFourTwo: checkFourTwo,
    checkRunTriple: checkRunTriple,
    checkTripleOne: checkTripleOne,
    checkAirplane: checkAirplane,
    checkTriple: checkTriple,
    checkRun: checkRun,
    checkPair: checkPair,
    checkPairs: checkPairs,
    checkSingle: checkSingle,
    checkAirplaneTwo: checkAirplaneTwo,
    checkKingBomb: checkKingBomb,
    checkTripleTwo: checkTripleTwo,
    checkFourTwoPair: checkFourTwoPair,
    getMaxCost: getMaxCost,
};